package com.ruoyi.personnel.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.personnel.mapper.DepartmentMapper;
import com.ruoyi.personnel.domain.Department;
import com.ruoyi.personnel.service.IDepartmentService;

/**
 * 部门管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-04
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService
{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询部门管理
     *
     * @param depId 部门管理主键
     * @return 部门管理
     */
    @Override
    public Department selectDepartmentByDepId(Integer depId)
    {
        return departmentMapper.selectDepartmentByDepId(depId);
    }

    /**
     * 查询部门管理列表
     *
     * @param department 部门管理
     * @return 部门管理
     */
    @Override
    public List<Department> selectDepartmentList(Department department)
    {
        return departmentMapper.selectDepartmentList(department);
    }

    /**
     * 新增部门管理
     *
     * @param department 部门管理
     * @return 结果
     */
    @Override
    public int insertDepartment(Department department)
    {
        department.setCreateTime(DateUtils.getNowDate());
        return departmentMapper.insertDepartment(department);
    }

    /**
     * 修改部门管理
     *
     * @param department 部门管理
     * @return 结果
     */
    @Override
    public int updateDepartment(Department department)
    {
        department.setUpdateTime(DateUtils.getNowDate());
        return departmentMapper.updateDepartment(department);
    }

    /**
     * 批量删除部门管理
     *
     * @param depIds 需要删除的部门管理主键
     * @return 结果
     */
    @Override
    public Boolean deleteDepartmentByDepIds(Integer[] depIds)
    {

        String[] flag = new String[depIds.length];
        for (int i = 0; i < depIds.length; i++) {
            List<Employee> employeeList = employeeMapper.selectEmployeeByDepId(depIds[i]);
            if (employeeList.size()>0){
                flag[i] = "false";
            }
        }
        if ( flag[0] != null){
           return false;
        }else {
            departmentMapper.deleteDepartmentByDepIds(depIds);
            return true;
        }
    }

    /**
     * 删除部门管理信息
     *
     * @param depId 部门管理主键
     * @return 结果
     */
    @Override
    public int deleteDepartmentByDepId(Integer depId)
    {
        return departmentMapper.deleteDepartmentByDepId(depId);
    }
}
